package com.charmzenus.it.zuul.filter;

import com.charmzenus.it.zuul.base.BaseZuulFilter;
import com.charmzenus.it.zuul.constants.FitlerNumber;
import com.charmzenus.it.zuul.utils.JsonUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

/**
 * 前置拦截器，统一拦截处理数据
 */
@Component
public class MessageFilter extends BaseZuulFilter {

    @Override
    public String filterType() {
        return PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FitlerNumber.order_message;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();

        //这里从url参数里获取, 也可以从cookie, header里获取
        String token = request.getParameter("message");
        if (org.springframework.util.StringUtils.isEmpty(token)) {
            errorHandler(requestContext, "message 不能为空");
        }
        return null;
    }
}
